package com.eenie.pwdharck;

import java.io.IOException;

public class EncodeUtil {


    private static final char[] a = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
    private static int[] b = new int[128];

    public static final byte MESSAGETYPE_NOTIFY = 63;

    static {
        for (int i = 0; i < b.length; i++) {
            b[i] = -1;
        }
        for (int i2 = 0; i2 < a.length; i2++) {
            b[a[i2]] = i2;
        }
    }

    public static String encode(byte[] bArr, int i, int i2) {
        int i3 = 0;
        char[] cArr = new char[(((i2 + 2) / 3) * 4)];
        int i4 = 0;
        while (true) {
            int i5 = i3;
            if (i4 >= cArr.length) {
                return new String(cArr);
            }
            a(bArr, i + i5, i2 - i5, cArr, i4);
            i3 = i5 + 3;
            i4 += 4;
        }
    }

    private static void a(char c, char c2, char c3, char c4, byte[] bArr, int i) throws IOException {
        int i2;
        int i3;
        char c5;
        int i4 = b[c & 127];
        int i5 = b[c2 & 127];
        if (c4 != '=') {
            i2 = b[c3 & 127];
            i3 = b[c4 & 127];
            c5 = 0;
        } else if (c3 == '=') {
            c5 = 2;
            i3 = 0;
            i2 = 0;
        } else {
            i2 = b[c3 & 127];
            c5 = 1;
            i3 = 0;
        }
        if (i4 < 0 || i5 < 0 || i2 < 0 || i3 < 0) {
            throw new IOException("Invalid character in Base64 string");
        }
        bArr[i] = (byte) (((i4 << 2) & 252) | ((i5 >>> 4) & 3));
        if (c5 < 2) {
            bArr[i + 1] = (byte) (((i5 << 4) & 240) | ((i2 >>> 2) & 15));
            if (c5 < 1) {
                bArr[i + 2] = (byte) (((i2 << 6) & 192) | (i3 & 63));
            }
        }
    }

    private static void a(byte[] bArr, int i, int i2, char[] cArr, int i3) {
        byte b2 = bArr[i];
        cArr[i3] = a[(b2 >>> 2) & 63];
        if (i2 > 2) {
            byte b3 = bArr[i + 1];
            byte b4 = bArr[i + 2];
            cArr[i3 + 1] = a[((b2 << 4) & 48) + ((b3 >>> 4) & 15)];
            cArr[i3 + 2] = a[((b3 << 2) & 60) + ((b4 >>> 6) & 3)];
            cArr[i3 + 3] = a[b4 & MESSAGETYPE_NOTIFY];
        } else if (i2 > 1) {
            byte b5 = bArr[i + 1];
            cArr[i3 + 1] = a[((b2 << 4) & 48) + ((b5 >>> 4) & 15)];
            cArr[i3 + 2] = a[((b5 << 2) & 60) + 0];
            cArr[i3 + 3] = '=';
        } else {
            cArr[i3 + 1] = a[((b2 << 4) & 48) + 0];
            cArr[i3 + 2] = '=';
            cArr[i3 + 3] = '=';
        }
    }

    public static byte[] decode(String str) throws IOException {
        return a(str, 0, str.length());
    }

    public static byte[] a(String str, int i, int i2) throws IOException {
        if (i2 % 4 != 0) {
            throw new IOException("Base64 string length is not multiple of 4");
        }
        int i3 = (i2 / 4) * 3;
        if (str.charAt((i + i2) - 1) == '=') {
            i3--;
            if (str.charAt((i + i2) - 2) == '=') {
                i3--;
            }
        }
        byte[] bArr = new byte[i3];
        int i4 = 0;
        for (int i5 = 0; i5 < i2; i5 += 4) {
            a(str.charAt(i + i5), str.charAt(i + i5 + 1), str.charAt(i + i5 + 2), str.charAt(i + i5 + 3), bArr, i4);
            i4 += 3;
        }
        return bArr;
    }
}
